<?php

class Pagination
{
    public function __construct ($obj) {
        $this->obj = $obj;
        $this->setPageObjCount(10);
        $this->setPageIndex(1);
    }

    public function pageCount () {
        $rlt = 0;
        $rlt += $this->objCount() / $this->pageObjCount();
        $rlt += ( $this->objCount() % $this->pageObjCount() ) > 0 ? 1 : 0;
        return (int)$rlt;
    }

    public function pageIndex () {
        return $this->page_index;
    }

    public function objCount () {
        return count($this->obj);
    }

    public function pageObjCount () {
        return $this->page_obj_count;
    }

    public function startObjIndex () {
        return ($this->pageIndex()-1) * $this->pageObjCount();
    }

    public function nextPageIndex () {
        if ($this->hasNextPageIndex()) {
            return $this->page_index + 1;
        } else {
            return $this->pageCount();
        }
    }

    public function prevPageIndex () {
        if ($this->hasPrevPageIndex()) {
            return $this->page_index - 1;
        } else {
            return 1;
        }
    }

    public function hasNextPageIndex () {
        return $this->page_index < $this->pageCount();
    }

    public function hasPrevPageIndex () {
        return $this->page_index > 1;
    }

    public function setPageIndex ($index) {
        if ($index < 1) {
            $this->page_index = 0;
        } else if ($index > $this->pageCount()) {
            $this->page_index = $this->pageCount();
        } else {
            $this->page_index = $index;
        }
         
    }

    public function setPageObjCount ($count) {
        $this->page_obj_count = $count;
    }

    public function page () {
        if ( count($this->obj) < 1 ) {
            return array();
        }
        $obj_count = $this->objCount();
        $start_index = $this->startObjIndex() >= $obj_count ? 
            0 : $this->startObjIndex();
        $end_index = ( $start_index + $this->pageObjCount() ) > $this->objCount() ? 
            $this->objCount() : ( $start_index + $this->pageObjCount() );
        $rlt = array_slice($this->obj, $start_index, $end_index - $start_index);


        return $rlt;
    }
}


